/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ambimmort.prserver.repository.webservice;

import com.ambimmort.prserver.bean.RepositoryOperationBean;
import com.ambimmort.prserver.bean.RepositoryOperationLogBean;
import com.ambimmort.prserver.bean.SVNFileBean;
import com.ambimmort.prserver.repository.Repository;

import javax.jws.WebService;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;


/**
 *
 * @author 定巍
 */
@WebService
public class PRMWebServiceImpl implements PRMWebService {

    @Override
    public void createRepository(String type, String instance) {
        Repository.getInstance(Repository.repositoryEntry("repo", type, instance), false);
    }

    @Override
    public List<InstanceBean> listRepositories(String type) {
        List<Repository> repos = Repository.getAllRepositorys();
        List<InstanceBean> repoNames = new ArrayList<InstanceBean>();
        for (Repository repo : repos) {
            if (repo.getEntry().getMessageType().equals(type)) {
                InstanceBean bean = new InstanceBean();
                bean.setType(type);
                bean.setInstance(repo.getEntry().getInstanceName());
                bean.setSerialNo(getHeadVersion(type, repo.getEntry().getInstanceName()));
                repoNames.add(bean);
            }
        }

        return repoNames;
    }

    @Override
    public List<String> listRepositoryTypes() {
        List<Repository> repos = Repository.getAllRepositorys();
        List<String> repoNames = new ArrayList<String>();
        for (Repository repo : repos) {
            if (!repoNames.contains(repo.getEntry().getMessageType())) {
                repoNames.add(repo.getEntry().getMessageType());
            }
        }
        return repoNames;
    }

    @Override
    public RepositoryOperationLogBean addPolicy(String type, String instance, String policy,String comment) {
        Repository repo = Repository.getInstance(Repository.repositoryEntry("repo", type, instance), false);
        return repo.getRepositoryKit().create(policy, comment);
    }

    @Override
    public RepositoryOperationLogBean updatePolicy(String type, String instance, int messageNo, String policy,String comment) {
        Repository repo = Repository.getInstance(Repository.repositoryEntry("repo", type, instance), false);
        return repo.getRepositoryKit().update(messageNo, policy, comment);
    }

    @Override
    public RepositoryOperationLogBean deletePolicy(String type, String instance, int messageNo) {
        Repository repo = Repository.getInstance(Repository.repositoryEntry("repo", type, instance), false);
        return repo.getRepositoryKit().delete(messageNo);
    }

    @Override
    public RepositoryOperationLogBean getMessage(String type, String instance, int messageNo) {
        Repository repo = Repository.getInstance(Repository.repositoryEntry("repo", type, instance), false);
        return repo.getRepositoryKit().getMessageWithLog(messageNo);
    }

    @Override
    public List<SVNFileBean> getHeadPolicies(String type, String instance) {
        Repository repo = Repository.getInstance(Repository.repositoryEntry("repo", type, instance), false);
        return repo.getRepositoryKit().checkOutHEAD();
    }

    @Override
    public List<RepositoryOperationBean> getUpdatingOperationBeans(String type, String instance, long startVersion) {
        Repository repo = Repository.getInstance(Repository.repositoryEntry("repo", type, instance), false);
        List<RepositoryOperationBean> actions = new ArrayList<RepositoryOperationBean>();
        TreeMap<Integer, RepositoryOperationBean> t = repo.getRepositoryKit().update(startVersion, repo.getHEAD());
        for (Integer k : t.keySet()) {
            actions.add(t.get(k));
        }
        return actions;
    }

    @Override
    public long getHeadVersion(String type, String instance) {
        Repository repo = Repository.getInstance(Repository.repositoryEntry("repo", type, instance), false);
        return repo.getRepositoryKit().getHeadVersionNumber();
    }

    @Override
    public List<RepositoryOperationLogBean> getMessageLogs(String type, String instance, int messageNo) {
        Repository repo = Repository.getInstance(Repository.repositoryEntry("repo", type, instance), false);
        return repo.getRepositoryKit().getMessageLogs(messageNo);
    }

    @Override
    public List<RepositoryOperationLogBean> getHeadPoliciesWithLog(String type, String instance, int pageCount, int page) {
        Repository repo = Repository.getInstance(Repository.repositoryEntry("repo", type, instance), false);
        List<SVNFileBean> beans = repo.getRepositoryKit().checkOutHEAD(pageCount, page);
        List<RepositoryOperationLogBean> logs = new ArrayList<RepositoryOperationLogBean>();
        for(SVNFileBean b:beans){
            logs.add(getMessage(type, instance, b.getMessageNo()));
        }
        return logs;
    }

    @Override
    public long getHeadPoliciesCount(String type, String instance) {
        Repository repo = Repository.getInstance(Repository.repositoryEntry("repo", type, instance), false);
        return repo.getRepositoryKit().checkOutHEADCount();
    }

    @Override
    public void deleteRepository(String type, String instance) {
        Repository.deleteInstance("repo", type, instance);
    }

}
